//
//  ZYQuickSetupVC.m
//  YiLin
//
//  Created by Zyark on 2018/6/5.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYQuickSetupVC.h"
#import "ZYModeSettingView.h"
#import "ZYImageUtils.h"
#import "ZYModeTypeModel.h"
#import "ZYCurveSelectView.h"
#import "ZYSocketMessageUtils.h"
#import "ZYDevice.h"
#import "ZYQuickSetupViewModel.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface ZYQuickSetupVC ()<ZYModeSettingViewDelegate>

/**
 节能模式
 */
@property(nonatomic,strong) ZYModeSettingView *energySavingModeView;

/**
 舒适模式
 */
@property(nonatomic,strong) ZYModeSettingView *comfortablgModeView;

/**
 自定义模式
 */
@property(nonatomic,strong) ZYModeSettingView *customModeView;


/**
 模式快捷背景
 */
@property(nonatomic,strong) UIView *modeView;

/**
 曲线快捷背景
 */
@property(nonatomic,strong) UIView *curveView;

/**
 模式快捷按钮
 */
@property(nonatomic,strong) UIButton *modeButton;

/**
  曲线快捷按钮
 */
@property(nonatomic,strong) UIButton *curveButton;


/**
 曲线设置
 */
@property(nonatomic,strong) ZYCurveSelectView *curveSelectView;

/**
系统推荐
 */
@property(nonatomic,strong) UILabel *systemCurveLabel;

/**
 获取默认值
 */
@property(nonatomic,strong) ZYQuickSetupViewModel *viewModel;
@end

@implementation ZYQuickSetupVC
typedef NS_ENUM(NSInteger,ZYQuickSetupButtonType){
    /**模式快捷*/
    ZYQuickSetupButtonTypeMode = 0,
    /**曲线快捷*/
    ZYQuickSetupButtonTypeCurve,
};

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_HOME_EDIT_DEVICE_QUICK_SETUP];
    self.fd_interactivePopDisabled = YES;
    [self.viewModel.modeDefaultCommand execute:[NSNumber numberWithUnsignedInteger:_device.serverDeviceType]];//获取默认值
    self.viewModel.mac = _device.mac;
    [self.viewModel.modeValueCommand execute:[NSNumber numberWithUnsignedInteger:_device.serverDeviceType]];//获取模式值
}

-(void)viewWillDisappear:(BOOL)animated{
    self.fd_interactivePopDisabled = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)setup{
    [self setupView];
    [self bindView];
}

-(void)setupView{
//    _modeButton = [self setupButton];
////    _curveButton = [self setupButton];
//    _modeButton.left = 15;
//    _modeButton.top = self.navigationView.bottom + 2;
    
//    _curveButton.centerY = _modeButton.centerY;
//    _curveButton.left = _modeButton.right;
    
//    _modeButton.tag = ZYQuickSetupButtonTypeMode;
//    _curveButton.tag = ZYQuickSetupButtonTypeCurve;
    
    _modeView = [[UIView alloc] init];
    _modeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_modeView];
    _modeView.size = CGSizeMake(ZYSCREEN_W, ZYSCREEN_H - self.navigationView.bottom - 10);
    _modeView.top = self.navigationView.bottom + 10;
    _modeView.left = 0;
    
//    _curveView = [[UIView alloc] initWithFrame:_modeView.frame];
//    [self.view addSubview:_curveView];
//    _curveView.hidden = YES;
    
    [self setupModeSettingView];
//    [self setupCurveSelectView];
}

-(void)setupModeSettingView{
    ZYModeTypeModel *energySavingModel = [[ZYModeTypeModel alloc] init];
    energySavingModel.modeType = ZYDeviceModeEnergySaving;
    energySavingModel.imageName = @"ic_home_mode_energy_saving_unselected";
    energySavingModel.title = LC_DEVICE_MODE_TYPE_ENERGY_SAVING;
    
    ZYModeTypeModel *comfortableModel = [[ZYModeTypeModel alloc] init];
    comfortableModel.modeType = ZYDeviceModeComfortable;
    comfortableModel.imageName = @"ic_home_mode_comfortable_unselected";
    comfortableModel.title = LC_DEVICE_MODE_TYPE_COMFORTABLE;
    
    ZYModeTypeModel *customModel = [[ZYModeTypeModel alloc] init];
    customModel.modeType = ZYDeviceModeCustom;
    customModel.imageName = @"ic_home_mode_custom_unselected";
    customModel.title = LC_DEVICE_MODE_TYPE_CUSTOM;
    
    _energySavingModeView = [[ZYModeSettingView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W, 100)];
    [_modeView addSubview:_energySavingModeView];
    _energySavingModeView.left = 0;
    _energySavingModeView.top = 0;
    _energySavingModeView.currentTemperature = 0;
    
    _comfortablgModeView = [[ZYModeSettingView alloc] initWithFrame:_energySavingModeView.bounds];
    [_modeView addSubview:_comfortablgModeView];
    _comfortablgModeView.top = _energySavingModeView.bottom;
    _comfortablgModeView.currentTemperature = 0;
    
//    _customModeView = [[ZYModeSettingView alloc] initWithFrame:_energySavingModeView.bounds];
//    [_modeView addSubview:_customModeView];
//    _customModeView.top = _comfortablgModeView.bottom;
//    _customModeView.currentTemperature = 0;
//    _customModeView.isNotEditable = YES;//不可编辑
    
    _energySavingModeView.model = energySavingModel;
    _energySavingModeView.delegate = self;
    _comfortablgModeView.model = comfortableModel;
    _comfortablgModeView.delegate = self;
//    _customModeView.model = customModel;
//    _customModeView.delegate = self;
    
    
    [self switchMode:_modeButton];
}

-(void)setupCurveSelectView{
    _systemCurveLabel = [[UILabel alloc] init];
    _systemCurveLabel.textColor = RGBA(71, 72, 74, 1);
    _systemCurveLabel.textAlignment = NSTextAlignmentLeft;
    _systemCurveLabel.size = CGSizeMake(ZYSCREEN_W / 2, 30);
    [_curveView addSubview:_systemCurveLabel];
    _systemCurveLabel.left = 15;
    _systemCurveLabel.top = 0;
    _systemCurveLabel.text =[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CURVE_TYPE_SYSTEM_LABEL];
    
    _curveSelectView = [[ZYCurveSelectView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W, 0)];
    [_curveView addSubview:_curveSelectView];
    _curveSelectView.top = _systemCurveLabel.bottom + 5;
    _curveSelectView.left = 0;
}

-(UIButton *)setupButton{
    UIButton *button = [[UIButton alloc] init];
    button.size =CGSizeMake((ZYSCREEN_W - 30 )/2, ZYSCREEN_W * 0.09);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button setTitleColor:kLabelColor forState:UIControlStateNormal];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:button.bounds] forState:UIControlStateSelected];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:button.bounds] forState:UIControlStateNormal];
    button.layer.cornerRadius = 10;
    button.layer.masksToBounds = YES;
    [button addTarget:self action:@selector(switchMode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    return button;
}

-(void)bindView{    
    //模式温度默认值
    @weakify(self);
    [[[self.viewModel.modeDefaultCommand executionSignals] switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        self.energySavingModeView.systemTemperature = [[x objectForKey:[NSString stringWithFormat:@"%ld",ZYDeviceModeEnergySaving]] floatValue];
        self.comfortablgModeView.systemTemperature = [[x objectForKey:[NSString stringWithFormat:@"%ld",ZYDeviceModeComfortable]] floatValue];
        self.customModeView.systemTemperature = [[x objectForKey:[NSString stringWithFormat:@"%ld",ZYDeviceModeCustom]] floatValue];
        if(self.energySavingModeView.currentTemperature == 0){
            self.energySavingModeView.currentTemperature = self.energySavingModeView.systemTemperature;
        }
        if(self.comfortablgModeView.currentTemperature == 0){
            self.comfortablgModeView.currentTemperature = self.comfortablgModeView.systemTemperature;
        }
        if(self.customModeView.currentTemperature == 0){
            self.customModeView.currentTemperature = self.customModeView.systemTemperature;
        }
    }];
    [self.viewModel.modeDefaultCommand.errors subscribeNext:^(id x) {
        @strongify(self);
        if([[NSErrorHelper handleErrorMessage:x] isEqualToString:LOGIN_FAILURE]){
            //登录失效
            [ZYBasicHelper handleErrorMessaeWithError:x message:nil controller:self];
        }
    }];
    //获取模式值
    [[[self.viewModel.modeValueCommand executionSignals] switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
//        NSMutableArray *curve = [NSMutableArray arrayWithArray:@[@YES,@YES,@YES,@YES]];
        for (ZYModeTypeModel *model in x) {
            switch (model.modeType) {
                case ZYDeviceModeEnergySaving:
                    self.energySavingModeView.currentTemperature = [model.value floatValue];
                    break;
                case ZYDeviceModeComfortable:
                    self.comfortablgModeView.currentTemperature = [model.value floatValue];
                    break;
                case ZYDeviceModeCustom:
                    self.customModeView.currentTemperature = [model.value floatValue];
                    break;
//                case 100 :
//                    //自动学习
//                    [curve setObject:[NSNumber numberWithBool:model.isOpen] atIndexedSubscript:0];
//                    break;
//                case 101 :
//                    [curve setObject:[NSNumber numberWithBool:model.isOpen] atIndexedSubscript:1];
//                    break;
//                case 102 :
//                    [curve setObject:[NSNumber numberWithBool:model.isOpen] atIndexedSubscript:2];
//                    break;
//                case 103 :
//                    [curve setObject:[NSNumber numberWithBool:model.isOpen] atIndexedSubscript:3];
//                    break;
            }
        }
    }];
    [self.viewModel.modeValueCommand.errors subscribeNext:^(id x) {
        @strongify(self);
        [ZYBasicHelper handleErrorMessaeWithError:x message:[NSErrorHelper handleErrorMessage:x] controller:self];
    }];
}

-(void)changeLanguage{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    [_curveButton setTitle:[language getStringByKey:LC_QUICK_SETUP_SETTING_CURVE_BTN_TITLE] forState:UIControlStateNormal];
    [_curveButton setTitle:[language getStringByKey:LC_QUICK_SETUP_SETTING_CURVE_BTN_TITLE] forState:UIControlStateSelected];
    
    [_modeButton setTitle:[language getStringByKey:LC_QUICK_SETUP_SETTING_MODE_BTN_TITLE] forState:UIControlStateNormal];
    [_modeButton setTitle:[language getStringByKey:LC_QUICK_SETUP_SETTING_MODE_BTN_TITLE] forState:UIControlStateSelected];
}

#pragma mark - ZYModeSettingViewDelegate
//温度改变
-(void)modeSettingView:(ZYModeSettingView *)modeSettingView changeTemperature:(CGFloat)temperature modeType:(NSUInteger)modeType{
    NSLog(@"modeType:%ld",modeType);
    [self.viewModel setModeValueWithDeviceType:_device.serverDeviceType andModel:modeType andParams:[NSString stringWithFormat:@"%.1f",temperature]];
//    NSDictionary *dict = @{@"lockStatus":@0,@"pannelModel":[NSNumber numberWithUnsignedInteger:modeType],@"powerStatus":@1,@"setTemperatureC2":[NSString stringWithFormat:@"%.1f",temperature]};
//    [ZYSocketMessageUtils setDeviceWithMac:_device.mac andParams:dict];
}

#pragma mark event
-(void)switchMode:(UIButton *)button{
    [UIView animateWithDuration:0.3 animations:^{
        BOOL isMode = button.tag == ZYQuickSetupButtonTypeMode;
        _modeButton.selected = isMode;
        _curveButton.selected = !isMode;
        _modeView.hidden = !isMode;
        _curveView.hidden = isMode;
    }];
}

#pragma mark - Getter
-(ZYQuickSetupViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYQuickSetupViewModel alloc] init];
    }
    return _viewModel;
}

@end
